package com.mqstudy.activemq.producer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mqstudy.common.model.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import javax.jms.Topic;

/**
 * ActiveMQ生产者
 */
@Slf4j
@Component
public class ActiveMQProducer {
    
    @Autowired
    private JmsTemplate jmsTemplate;
    
    @Autowired
    private ObjectMapper objectMapper;
    
    @Autowired
    private Queue testQueue;
    
    @Autowired
    private Topic testTopic;
    
    /**
     * 发送队列消息
     */
    public void sendQueueMessage(Message message) {
        try {
            log.info("发送队列消息: {}", message);
            String messageBody = objectMapper.writeValueAsString(message);
            jmsTemplate.convertAndSend(testQueue, messageBody);
            log.info("队列消息发送成功");
        } catch (JsonProcessingException e) {
            log.error("消息序列化失败: {}", e.getMessage(), e);
            throw new RuntimeException("消息序列化失败", e);
        }
    }
    
    /**
     * 发送主题消息
     */
    public void sendTopicMessage(Message message) {
        try {
            log.info("发送主题消息: {}", message);
            String messageBody = objectMapper.writeValueAsString(message);
            jmsTemplate.convertAndSend(testTopic, messageBody);
            log.info("主题消息发送成功");
        } catch (JsonProcessingException e) {
            log.error("消息序列化失败: {}", e.getMessage(), e);
            throw new RuntimeException("消息序列化失败", e);
        }
    }
    
    /**
     * 发送持久化消息
     */
    public void sendDurableMessage(Message message) {
        try {
            log.info("发送持久化消息: {}", message);
            String messageBody = objectMapper.writeValueAsString(message);
            jmsTemplate.convertAndSend(testQueue, messageBody, msg -> {
                msg.setJMSDeliveryMode(javax.jms.DeliveryMode.PERSISTENT);
                return msg;
            });
            log.info("持久化消息发送成功");
        } catch (JsonProcessingException e) {
            log.error("消息序列化失败: {}", e.getMessage(), e);
            throw new RuntimeException("消息序列化失败", e);
        }
    }
    
    /**
     * 发送批量消息
     */
    public void sendBatchMessages(int count) {
        log.info("发送批量消息，数量: {}", count);
        
        for (int i = 1; i <= count; i++) {
            Message message = Message.builder()
                    .messageId("batch-" + i)
                    .topic("batch-topic")
                    .content("批量消息内容 " + i)
                    .messageType("BATCH")
                    .key("batch-key-" + i)
                    .build();
            
            try {
                sendQueueMessage(message);
            } catch (Exception e) {
                log.error("发送批量消息失败 - 序号: {}, Error: {}", i, e.getMessage());
            }
        }
    }
} 